package com.ksr.logistics.service.impl;

import com.github.pagehelper.PageHelper;
import com.ksr.logistics.mapper.BaseDataMapper;
import com.ksr.logistics.pojo.*;
import com.ksr.logistics.service.BaseDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaseDataServiceImpl implements BaseDataService {

    @Autowired
    private BaseDataMapper baseDataMapperMapper;

    @Override
    public List<BaseData> selBaseDatas() {
        BaseDataExample example = new BaseDataExample();
        return baseDataMapperMapper.selectByExample(example);
    }

    @Override
    public List<BaseData> selPageBaseData(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer pageSize,
            String keyWord) {
        BaseDataExample example = new BaseDataExample();
        if (StringUtils.isNotBlank(keyWord)){
            BaseDataExample.Criteria criteria = example.createCriteria();
            criteria.andBaseNameLike("%"+keyWord+"%");
        }
        PageHelper.startPage(page,pageSize);
        List<BaseData> baseDatas = baseDataMapperMapper.selectByExample(example);
        return baseDatas;
    }

    @Override
    public Result deleteById(Long id) {
        Result result=new Result();
        List<BaseData> baseDatas=selChilds(id);
        if (baseDatas.size()>0){
            result.setCode(0);
            result.setMsg("该数据有正在使用的子数据<br>请删除子数据后重试！");
            return result;
        }else {
            int res= baseDataMapperMapper.deleteByPrimaryKey(id);
            if (1==res){
                result.setCode(1);
                result.setMsg("数据删除成功！");
            }else{
                result.setCode(0);
                result.setMsg("数据删除失败,请重试！");
            }
            return result;
        }
    }

    @Override
    public Result deleteByIds(List<Long> ids) {
        Result result=new Result();
        int res= 0;
        int count=ids.size();
        for (int i = 0; i < count; i++) {
            if (ids.size()==0){
                break;
            }
            List<Long> newIds=new ArrayList<>();
            for (Long id:ids) {
                if (1==deleteById(id).getCode()){
                    res++;
                   newIds.add(id);
                }
            }
            if (newIds.size()>0){
                ids.removeAll(newIds);
            }
        }
        if (1<=res){
            result.setCode(1);
            if (count==res){
                result.setMsg("成功删除所有选中数据！");
            }else{
                result.setMsg("成功删除数据"+res+"个<br>部分数据有正在使用的子数据,请删除子数据后重试！");
            }
        }else{
            result.setCode(0);
            result.setMsg("被选择数据有正在使用的子数据<br>请删除子数据后重试！");
        }
        return result;
    }

    @Override
    public BaseData selById(Long id) {
        return baseDataMapperMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertBaseData(BaseData baseData) {
        return baseDataMapperMapper.insert(baseData);
    }

    @Override
    public int updateBaseData(BaseData baseData) {
        return baseDataMapperMapper.updateByPrimaryKeySelective(baseData);
    }

    @Override
    public List<BaseData> selByBaseName(String name) {
        BaseDataExample example = new BaseDataExample();
        BaseDataExample.Criteria criteria = example.createCriteria();
        criteria.andBaseNameEqualTo(name);
        return baseDataMapperMapper.selectByExample(example);
    }

    @Override
    public List<BaseData> selChilds(Long id) {
        BaseDataExample example = new BaseDataExample();
        BaseDataExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        return baseDataMapperMapper.selectByExample(example);
    }

    @Override
    public List<BaseData> selParentData() {
        BaseDataExample example = new BaseDataExample();
        BaseDataExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdIsNull();
        return baseDataMapperMapper.selectByExample(example);
    }

    @Override
    public List<BaseData> selBaseDataByParentName(String name) {
        List<BaseData> baseData = baseDataMapperMapper.selBaseDataByParentName(name);
        return baseData;
    }
}
